# -*- coding: utf-8 -*-

def Singleton(cls):
    _instance = {}

    def _singleton(*args, **kwargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kwargs)
        return _instance[cls]

    return _singleton


def SingletonWithParams(cls):
    _instance = {}

    def _singleton_with_params(*args, **kwargs):
        kwargs_key = []
        if kwargs:
            keys = kwargs.keys()
            keys.sort()
            for key in keys:
                kwargs_key.append("%s=%s" % (key, kwargs[key]))
        unique_key = (cls, args, tuple(kwargs_key))
        if unique_key not in _instance:
            _instance[unique_key] = cls(*args, **kwargs)
        return _instance[unique_key]

    return _singleton_with_params
